<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>集合操作</title>
</head>
<body>

</body>
<script>


  var employees=[];
  var emp0 = {
    id:0,
    name:'张蓝',
    gender:'female',
    dept:'dev',
    job:'经理',
    salary:15000
  }


  /**
   *  q1: 添加一个新员工，基本信息自拟，gender是 male，部门 dev
   *  q2: 在集合头部添加一个员工  ---- gender female, 部门 hr
   *  q3: 添加4条随机数据
   *  q4: 获取所有gender male 员工，然后 求出他们的工资总和 、平均数
   *  q5: 获取所有dev 员工，工资 +200
   *  .........
   * */

  var emp1 = {
    id:1,
    name:'张Sam',
    gender:'male',
    dept:'dev',
    job:'经理',
    salary:15000
  }

 // employees.push(emp1);

  employees.push({
    id:1,
    name:'张Sam',
    gender:'male',
    dept:'dev',
    job:'副经理',
    salary:12000
  })


  employees.unshift({
    id:2,
    name:'张紫叶',
    gender:'female',
    dept:'hr',
    job:'普通员工',
    salary:9000
  })

/**
 * 重复4遍，然后修改信息
 * (不要重复制造轮子)
 * */
  // employees.unshift({
  //   id:2,
  //   name:'张紫叶',
  //   gender:'female',
  //   dept:'hr',
  //   job:'普通员工',
  //   salary:9000
  // })

  for(var i=0;i<4;i++){
    employees.push({
      id:3+i,
      name:'姓名'+i,
      gender:i%3==0?'male':'female',
      dept:'dev',
      job:'副经理',
      salary:2500+(i*1800)
    })

  }
  console.log(employees);

 var males= employees.filter(function (emp){
      return emp.gender=='male';
  })

 var salary= employees.filter(function (emp){
      return emp.gender=='male';
  }).map(function (emp){
      return emp.salary;
 }).reduce(function (prev,curr){
     return prev+curr;
 })

  console.log(salary);
  console.log(salary/males.length);


var updateDev=  employees.filter(function (emp){
      return emp.dept=='dev';
  }).map(function (emp){
       return {
            id:emp.id,
            name:emp.name,
            dept:emp.dept,
            oldSalary:emp.salary,
            salary:emp.salary+200
       }
  });

console.log(updateDev);

// random ; mockjs -- 产生模拟数据--随机属性

</script>
</html>